package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public PageResult getPage(SetmealPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        Page<Setmeal> page = setMealMapper.getPage(queryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.insertSetmeal(setmeal);
        setMealMapper.insertSetmealDishs(setmeal.getId(), setmealDTO.getSetmealDishes());

    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //先根据套餐id更新套餐
        setMealMapper.updateSetmeal(setmeal);
        //如果套餐没有菜品则直接返回
        if (setmealDTO.getSetmealDishes() == null || setmealDTO.getSetmealDishes().isEmpty()) {
            return;
        }
        //初始化ids数组
        long ids[] = {setmeal.getId()};


        //根据套餐id删除套餐菜品
        setMealMapper.deleteSetmealDishs(ids);
        //重新插入套餐菜品
        setMealMapper.insertSetmealDishs(setmeal.getId(), setmealDTO.getSetmealDishes());
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setMealMapper.updateSetmeal(setmeal);
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setMealMapper.getSetmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setMealMapper.getSetmealDishsBySetmealId(id));
        return setmealVO;
    }


    @Override
    public void delete(long[] ids) {
        //如果套餐为启售状态，抛出异常
        for (long id : ids) {
            Setmeal setmeal = setMealMapper.getSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
            setMealMapper.deleteSetmealDishs(ids);
            setMealMapper.deleteSetmeals(ids);
    }

    @Override
    public List<SetmealVO> getSetmealsByCategoryId(Long categoryId) {
        return setMealMapper.getSetmealsByCategoryId(categoryId);
    }

    @Override
    public List<DishItemVO> getDishBySetMealId(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }

}
